using System;
using UnityEngine;
using System.Collections;

public class MoveShape : MonoBehaviour
{
	
//	public Shader shader = Shader.Find("Transparent/Diffuse");
	public static int MOUSE_RIGHT_CLICK = 0;
	Vector3 screenPointFromShape;
	Vector3 offset;
	float initMouseX;
	bool onDrag;
	GameObject goSelected;
	
	// Use this for initialization
	void Start ()
	{
	
	}
	
	// Update is called once per frame
	void Update ()
	{
		manageObjectMoveWithMouseEvent();
	}
	
	void manageObjectMoveWithMouseEvent () {
				if (Input.GetMouseButtonDown (MOUSE_RIGHT_CLICK)) {
			
			goSelected = getObjectSelected ();
			
			if (goSelected != null) {
				
				Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
				RaycastHit hit;
				
				int layerMask = 1 << 8;
				
				if (Physics.Raycast (ray, out hit, 100, layerMask)) {
				
					Debug.DrawLine (ray.origin, hit.point);
					
					offset = hit.point - goSelected.transform.position;
					//offset.y = transform.position.y;
					
					
				}
				initMouseX = Input.mousePosition.x;
				onDrag = true;
				
				Color objectColor = goSelected.renderer.material.color;
				objectColor.a = 0.5f;
				goSelected.renderer.material.color = objectColor;
			}
			
		}
	
		// Toujours appelé lorsque clique droit est préssé:
		if (Input.GetMouseButton (MOUSE_RIGHT_CLICK)) {
	
			if (goSelected != null) {
				Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
				RaycastHit hit;
			
				int layerMask = 1 << 8;
			
				if (Physics.Raycast (ray, out hit, 100, layerMask)) {
					Debug.DrawLine (ray.origin, hit.point);
					goSelected.transform.position = hit.point - offset;//+ new Vector3(0, transform.localScale / 2, 0);//offset;
				}
			}
//			Debug.Log ("GetMouseButton ....");
		}
	
		// Moment unique lors du "release" du clique droit
		if (Input.GetMouseButtonUp (MOUSE_RIGHT_CLICK)) {
			onDrag = false;
			Color objectColor = renderer.material.color;
			objectColor.a = 1;
			renderer.material.color = objectColor;
		
		}
	}
	
	GameObject getObjectSelected ()
	{
		
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit hit;
		
		if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
			
			String tagGo = hit.collider.gameObject.tag;
			
			if (tagGo.Equals ("slab") || tagGo.Equals ("otherslab")) {
				return hit.collider.gameObject;	
			}
		}
		return null;
	}
	
	/*
	void OnMouseDown ()
	{
		
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit hit;
		
		int layerMask = 1 << 8;
		
		if (Physics.Raycast (ray, out hit, 100, layerMask)) {
		
			Debug.DrawLine (ray.origin, hit.point);
			offset = hit.point - transform.position;
			//offset.y = transform.position.y;
		}
		initMouseX = Input.mousePosition.x;
		onDrag = true;
		
		
		Color objectColor = renderer.material.color;
		objectColor.a = 0.5f;
		renderer.material.color = objectColor;
	}
	
	void OnMouseUp ()
	{
		onDrag = false;
		Color objectColor = renderer.material.color;
		objectColor.a = 1;
		renderer.material.color = objectColor;
	}
	
	void OnMouseDrag ()
	{
		
		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		RaycastHit hit;
		
		int layerMask = 1 << 8;
		
		if (Physics.Raycast (ray, out hit, 100, layerMask)) {
			Debug.DrawLine (ray.origin, hit.point);
			transform.position = hit.point - offset;//+ new Vector3(0, transform.localScale / 2, 0);//offset;
		}
	}

	void OnTriggerEnter (Collider other)
	{
	}*/
}
